package CommunityController;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.PagingManager;


public class BCBoardAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
    BCBoardDAO bCBoardDAO =new BCBoardDAO();
    private static final String sessionID  = null;
	private HttpSession session = null;
 
    public BCBoardAction() {
        super();
        // TODO Auto-generated constructor stub
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	private int currentPage=1;
	
 	private PagingManager paging;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String action=request.getParameter("action");
		BCBoardBean bCBoardBean=null;
		RequestDispatcher view = null;
		PrintWriter out = response.getWriter(); 
		
		if(action.equals("cm_bc_list_main")){ //노인페이지메인화면사진2
			
			ArrayList list =bCBoardDAO.cm_bc_list_main();
			request.setAttribute("list", list);
			request.getRequestDispatcher("/Main/OldMain/home_board2.jsp").forward(request, response);
		}
		
		else if(action.equals("cm_bc_list")){ //babycare게시판 메인list
			
			String mem_nick=request.getParameter("nick");//로그인아이디
			int level=bCBoardDAO.young_level(mem_nick);//회원레벨체크
		    String searchword=request.getParameter("searchword");//검색단어
		    String search=request.getParameter("search");//검색키워드
		    
		    if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
				currentPage=Integer.parseInt(request.getParameter("currentPage"));
			}
		    
			HashMap param=new HashMap();//페이징처리위한
			param = new HashMap<String, Object>();
			param.clear();
			param.put("pageSize", 8 );
			param.put("currentPage", currentPage );
			param.put("pageGroupSize", 5 );
			
			param.put("searchword", searchword);
			param.put("search",search);
			
			int pageNo = ((Integer)param.get("currentPage") -1) * (Integer)param.get("pageSize");
			int endNum = pageNo + (Integer)param.get("pageSize");
			param.put( "startNum", pageNo );
			ArrayList list =bCBoardDAO.cm_bc_list(param);
			int totalCount = bCBoardDAO.bcTotalCount(param);
			param.put("totalCount", totalCount);
			paging=new common.PagingManager();
			paging.setPageNo(param);
				
			
			request.setAttribute("list", list);
			request.setAttribute("paging", paging.getReturnHtml());
			request.setAttribute("page","/community/BabyCare/CM_Babycare_List");
			
			if(level==2){
				view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");	
			}else if(level==1){
				view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
			}else if(level==3){
				view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
			}
			view.forward(request, response);
				
		}
		
		else if(action.equals("cm_bc_read")){ // 게시판글읽기
			
		   String mem_nick=request.getParameter("nick");
		   int level=bCBoardDAO.young_level(mem_nick);
		   int id=Integer.parseInt(request.getParameter("id"));
		   bCBoardBean=new BCBoardBean();
		   bCBoardBean= bCBoardDAO.cm_bc_read(id);
		   
		 
		   request.setAttribute("bCBoardBean", bCBoardBean);
		   request.setAttribute("page","/community/BabyCare/CM_Babycare_Read");
		   
		   if(level==2){
				view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");				
		   }else if(level==1){
				view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
		   }else if(level==3){
			   view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
		   }
		   view.forward(request, response);
			
		}
		
		else if(action.equals("cm_bc_write_form")){ //게시판 글쓰기폼
			
		   String mem_nick=request.getParameter("nick");
		   int level=bCBoardDAO.young_level(mem_nick);
		  
		   request.setAttribute("page","/community/BabyCare/CM_Babycare_Write");
		   
		   if(level==2){
				view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");	
		   }else if(level==1){
				view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
		   }else if(level==3){
			   view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
		   }
		   view.forward(request, response);
		}
		
		else if(action.equals("cm_bc_write")){ //게시판 글등록하기
			
		   String mem_nick=request.getParameter("nick");
		   bCBoardBean=new BCBoardBean();
		   bCBoardBean.setBc_title(request.getParameter("bc_title"));
		   bCBoardBean.setBc_contents(request.getParameter("bc_contents"));
		   bCBoardBean.setBc_nick(request.getParameter("bc_nick"));
		   bCBoardDAO.cm_bc_write(bCBoardBean);
		   out.println( "<script>" );
		   out.println( "location.href=\"/KOMORIUTA/BCBoardAction.do?action=cm_bc_list&nick="+mem_nick+"\";" );
	       out.println( "</script>" ); 
		}
		
		else if(action.equals("cm_bc_modify_form")){ //글수정폼
			
		   String mem_nick=request.getParameter("nick");
		   int level=bCBoardDAO.young_level(mem_nick);
		   int id=Integer.parseInt(request.getParameter("id"));
		   bCBoardBean=bCBoardDAO.cm_bc_read(id);
			
		   session = request.getSession();
		   session.setAttribute("bCBoardBean", bCBoardBean);
		   request.setAttribute("page","/community/BabyCare/CM_Babycare_Modify");
		   if(level==2){
				view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");		
		   }else if(level==1){
				view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
		   }
		   view.forward(request, response);
		}
		
		else if(action.equals("cm_bc_modify")){ //글수정하기
			
			String mem_nick=request.getParameter("nick");
		    bCBoardBean=new BCBoardBean();
			int id=Integer.parseInt(request.getParameter("id"));
			bCBoardBean.setId(id);
			bCBoardBean.setBc_title(request.getParameter("bc_title"));
			bCBoardBean.setBc_contents(request.getParameter("bc_contents"));
			bCBoardDAO.cm_bc_modify(bCBoardBean);
			out.println( "<script>" );
			out.println( "location.href=\"/KOMORIUTA/BCBoardAction.do?action=cm_bc_list&nick="+mem_nick+"\";" );
		    out.println( "</script>" ); 
		}
		
		else if(action.equals("cm_bc_delete")){ //글삭제
		   String mem_nick=request.getParameter("nick");
		   int id=Integer.parseInt(request.getParameter("id"));
		   bCBoardDAO.cm_bc_delete(id);
		   out.println( "<script>" );
		   out.println( "location.href=\"/KOMORIUTA/BCBoardAction.do?action=cm_bc_list&nick="+mem_nick+"\";");
		   out.println( "</script>" ); 
		}
		
		else if(action.equals("cm_bc_reple_form")){ //리플폼
			
		   String mem_nick=request.getParameter("nick");
		   int level=bCBoardDAO.young_level(mem_nick);
		   
		   request.setAttribute("page","/community/BabyCare/CM_Babycare_Rwrite");
		   
		   if(level==2){	
				view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");	
		   }else if(level==1){
				view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
		   }else if(level==3){
			   view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
		   }
			view.forward(request, response);
		}
		 
		else if(action.equals("cm_bc_reple")){//리플달기
		    String mem_nick=request.getParameter("nick");
			int level=bCBoardDAO.young_level(mem_nick);
			bCBoardBean=new BCBoardBean();
			bCBoardBean.setId(Integer.parseInt(request.getParameter("id")));
			bCBoardBean.setBc_title(request.getParameter("title"));
			bCBoardBean.setBc_contents(request.getParameter("contents"));
			bCBoardBean.setBc_nick(request.getParameter("nick"));
			bCBoardBean.setRef(Integer.parseInt(request.getParameter("ref")));
			bCBoardBean.setRe_step(Integer.parseInt(request.getParameter("re_step")));
			bCBoardBean.setRe_level(Integer.parseInt(request.getParameter("re_level")));
		   
			bCBoardDAO.cm_bc_reple(bCBoardBean);
		    out.println( "<script>" );
			out.println( "location.href=\"/KOMORIUTA/BCBoardAction.do?action=cm_bc_list&nick="+mem_nick+"\";" );
			out.println( "</script>" ); 
		}
	}
}
